import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/views/Layout.vue'
import home from '@/views/home'
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/',
        component: home
      },
      { path: 'category/:id', component: () => import('@/views/category/index.vue') },
      { path: 'category/sub/:id', component: () => import('@/views/category/sub.vue') },
      { path: 'product/:id', component: () => import('@/views/goods/index.vue') }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior (to, from, savedPosition) {
    // 首页刷新不用跳回顶部
    if (to.path === '/') return savedPosition
    // 始终滚动到顶部
    return { top: 0 }
  }
})

export default router
